package com.wen.springbootorderrabbitmqproducer.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TtlConfig {

    @Bean
    public DirectExchange directTtlExchange(){
        return new DirectExchange("direct_ttl_exchange",true,false,null);
    }

    @Bean
    public Queue ttlQueue(){
        Map<String,Object> arges = new HashMap<>();

        //队列内消息过期，获取长度满足之后，都会进入死信队列

        //队列内过期时间
        arges.put("x-message-ttl",5000);
        //队列长度
        arges.put("x-max-length",5);
        //死信交换机
        arges.put("x-dead-letter-exchange","dead_direct_exchange");
        //routingkey
        arges.put("x-dead-letter-routing-key","dead");
        return new Queue("ttl_queue",true,false,false,arges);
    }

    @Bean
    public Queue ttlMessageQueue(){
        return new Queue("ttl_message_queue",true);
    }

    //队列内消息过期队列
    @Bean
    public Binding ttlBind(){
        return BindingBuilder.bind(ttlQueue()).to(directTtlExchange()).with("ttl");
    }

    //指定消息过期队列
    @Bean
    public Binding ttlMessage(){
        return BindingBuilder.bind(ttlMessageQueue()).to(directTtlExchange()).with("ttl_message");
    }

}
